import dict from '../common/core/dict';
import formInfo from '@ohos.app.form.formInfo';
import formItem from '../common/model/common/formItem';
import formProvider from '@ohos.app.form.formProvider';
import serviceUntils from '../common/card/serviceUntils';
import relationalStore from '@ohos.data.relationalStore';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';

export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want) {
    let formId: string = want.parameters[dict.IDENTITY_KEY] as string;
    let formName: string = want.parameters[dict.NAME_KEY] as string;
    let dimensionFlag: number = want.parameters[dict.DIMENSION_KEY] as number;
    serviceUntils.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      let form: formItem = new formItem();
      form.formId = formId;
      form.formName = formName;
      form.dimension = dimensionFlag;
      serviceUntils.insertForm(form, rdbStore);
    });
    formProvider.setFormNextRefreshTime(formId, dict.FIVE_MINUTES);
    return formBindingData.createFormBindingData();
  }

  onUpdateForm(formId) {
    serviceUntils.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      serviceUntils.updateCardData(rdbStore);
    });
    formProvider.setFormNextRefreshTime(formId, dict.FIVE_MINUTES);
  }

  onRemoveForm(formId: string) {
    serviceUntils.createRdbStore(this.context).then((rdbStore: relationalStore.RdbStore) => {
      serviceUntils.deleteFormData(formId, rdbStore);
    });
  }

  onAcquireFormState(want) {
    return formInfo.FormState.READY;
  }
}